package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel otherAccountLabel = new JLabel("请输入对方账号");
    private JLabel blanceLabel = new JLabel("请输入转账金额");
    private JTextField otherAccountTextField = new JTextField();
    private JTextField blanceTextField = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton backBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);
    public TransferPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        otherAccountLabel.setBounds(300,200,200,50);
        otherAccountLabel.setFont(font);
        this.add(otherAccountLabel);

        otherAccountTextField.setBounds(500,200,200,50);
        otherAccountTextField.setFont(font);
        this.add(otherAccountTextField);

        blanceLabel.setBounds(300,250,200,50);
        blanceLabel.setFont(font);
        this.add(blanceLabel);

        blanceTextField.setBounds(500,250,200,50);
        blanceTextField.setFont(font);
        this.add(blanceTextField);

        sureBtn.setBounds(350,350,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        backBtn.setBounds(520,350,140,50);
        backBtn.addActionListener(this);
        this.add(backBtn);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sureBtn){
            String otherAccount = otherAccountTextField.getText();
            double blance = Double.parseDouble(blanceTextField.getText());
            User loginUser = atm05.getLoginUser();
            if(loginUser.getAccount().equals(otherAccount)){
                JOptionPane.showMessageDialog(this,"不能转给自己");
                return;
            }
            boolean flag = false;
            int otherIndex = 0;
            for(int i = 0;i < atm05.getUsers().size();i++){
                if(otherAccount.equals(atm05.getUsers().get(i).getAccount())){
                    flag = true;
                    otherIndex = i;
                    break;
                }
            }
            if(!flag){
                JOptionPane.showMessageDialog(this,"账号不存在");
                return;
            }
            if(loginUser.getBlance() < blance){
                JOptionPane.showMessageDialog(this,"余额不足");
                return;
            }
            loginUser.setBlance(loginUser.getBlance() - blance);
            atm05.getUsers().get(otherIndex).setBlance(atm05.getUsers().get(otherIndex).getBlance() + blance);
            FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
            JOptionPane.showMessageDialog(this,"转账成功");

        }
        otherAccountTextField.setText("");
        blanceTextField.setText("");
        atm05.showPanel("menu");
    }
}
